<?php
// 后台首页

define('IN_MMOA', true);

require dirname(__FILE__).'/includes/init.php';

if ($_REQUEST['act'] == '')
{
	if (!empty($_SESSION['admin_id'])){
		header("Location: ./?act=frame");
	}
	
	if(!empty($_SESSION['admin_login_error'])){
		$smarty->assign('adminLoginError', '1');
		$_SESSION['admin_login_error'] = '0';
	}
	//$smarty->assign('fg', '../');
	$smarty->assign('mm_show', 'admin_login');

	clear_caches();
	$smarty->display('index.htm');
}
//登录判断是否登录成功
elseif ($_REQUEST['act'] == 'admin_login')
{
	$name = trim($_POST['admin_name']);
	$password = md5($_POST['admin_pwd']);
	//$department = trim($_POST['department']);
	//echo $name; exit();
	
	$admin_id = admin_login_check($name, $password);
	//echo $admin_id; exit();

	if (!empty($admin_id))
	{
		$_SESSION['admin_name'] = $name;
		$_SESSION['admin_id'] = $admin_id;
		//echo $admin_id;
		//$_SESSION['department'] = $department;
		header("Location: ./?act=frame");
	}
	else
	{
		//echo 'Login Error. Please check out you name, password and department selected!';
		//echo '<br>';
		//echo '<a href="./">back</a>';
		$_SESSION['admin_login_error'] = '1';
		echo 'error';
		header("Location: ./");
	}
}

// 显示框架
elseif ($_REQUEST['act'] == 'frame')
{
	if (empty($_SESSION['admin_id'])){
		header("Location: ./");
	}
	
	$smarty->assign('mm_show', 'frame');
	
	clear_caches();
	$smarty->display('index.htm');
}
// 框架顶部
elseif ($_REQUEST['act'] == 'top')
{
	$smarty->assign('admin_name', $_SESSION['admin_name']);
	$smarty->assign('mm_show', 'admin_top');
	
	clear_caches();
	$smarty->display('top.htm');
}
// 框架左边
elseif ($_REQUEST['act'] == 'menu')
{
	//echo 'menu'; exit;
	$smarty->assign('mm_show', 'admin_menu');
	
	clear_caches();
	$smarty->display('menu.htm');
}
// 框架主区域
elseif ($_REQUEST['act'] == 'main')
{
	$smarty->assign('admin_name', $_SESSION['admin_name']);
	$smarty->assign('mm_show', 'admin_main');
	
	clear_caches();
	$smarty->display('main.htm');
}

//*************************-- FUNCTIONS
function admin_login_check($name, $password)
{	
	$sql = "SELECT id FROM " . $GLOBALS['mmoa']->table('employees') .
	" where e_name = '" . $name . "' AND e_pass = '" . $password . "' AND is_admin = 1";
	$admin_id = $GLOBALS['db']->getOne($sql);

	if ($admin_id)
	{
		return $admin_id;
	}
	else
	{
		return false;
	}
}
?>